require(["../config"],function(){
	require(["jquery","header","template","bootstrap"],function($,header,template){
		function Index(){
			this.newPosition = `
			<div class="modal fade" id="ModalPosition" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
							<h4 class="modal-title" id="myModalLabel">职位信息</h4>
						</div>
						<div class="modal-body">
						<div class="alert alert-success hidden add-succ text-center" role="alert">提交成功！</div>
							<div class="modal-body">
								<form class="position-form">
									<div class="form-group">
										<label for="recipient-name" class="control-label">公司logo:</label>
										<input type="file" name="logo" class="form-control" id="recipient-name">
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">职位名称:</label>
										<input type="text" name="position" class="form-control" placeholder="请输入职位名"></input>
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">公司名称:</label>
										<input type="text"  name="company" class="form-control" placeholder="请输入公司名称验"></input>
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">工作经验:</label>
										<input type="text"  name="exprience" class="form-control" placeholder="请输入工作经验"></input>
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">职位类型</label>
										<input type="text"  name="type" class="form-control" placeholder="请输入职位名称"></input>
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">工作地点:</label>
										<input type="text"  name="address" class="form-control" placeholder="请输入工作地点"></input>
									</div>
									<div class="form-group">
										<label for="message-text" class="control-label">岗位薪资</label>
										<input type="text"  name="salary" class="form-control" placeholder="请输入岗位薪资"></input>
									</div>
								</form>
							</div>
						</div>
						<div class="modal-footer">
						    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
							<button type="button" class="btn btn-primary btn-tg">提交</button>
						</div>
					</div>
				</div>
			</div>`
			this.createDom().addListener();
		}
		$.extend(Index.prototype,{
			createDom(){
				$("body").append(this.newPosition);
				/*$.getJSON("http://rap2api.taobao.org/app/mock/119565/list", (data)=>{
					console.log(data.arr);
					var html = template("list-template", {position: data.arr});
					$("#tbody").html(html);
				});*/
				$.post("/api/position/find",res=>{
					var html = template("list-template", {position:res.res_body.data});
					console.log(res.res_body.data)
					$("#tbody").html(html);
				})
				return this;
			},
			addListener(){
				$(".btn-tg").on("click",this.addHandle);
				$("tbody").on("click",".delBtn",this.delHandle);
				$("tbody").on("click",".editBtn",this.dataHandle);
				$(".continer-fluid").on("click",".btn-bc",this.editHandle);
			},
			addHandle(){
				const url = "/api/position/add";
				//获得表单信息序列字符串
				/*const data = $(".position-form").serialize();
				console.log(data);*/
				/*$.post(url,data,res=>{
					console.log(res);
				})*/
				//包装需要上传到服务器的数据（包括文本、二进制流）
				const formData = new FormData($(".position-form")[0]);
				console.log(formData);
				$.ajax({
					url:url,
					type:"post",
					data:formData,
					processData:false,
					contentType:false,
					success:function(res){
						console.log(res)
						if(res.res_body.status === 1){
							$(".add-succ").removeClass("hidden");
							function dissp(){
								$("#ModalPosition").modal('hide');
								window.location.reload();
							}
							setTimeout(dissp, 1000);
						}
					}
				})
			},
			delHandle(e){
				if (confirm("确认删除吗？")) {
					$(e.target).parents("tr").remove();
					const url = "/api/position/del",
					 	 data = $(e.target).parent().siblings(".position").html();
					 	 console.log(data);
					$.post(url,"position="+data,res=>{
						if(res.res_body.data.ok === 1){
							alert("删除成功")
							window.location.reload();
						};
					})	
				}
			},
			dataHandle(e){
				var $tr = $(e.target).parents("tr");
				$("#_id").val($tr.find("._id").text());
				$("#logo").val($tr.find(".logo").text());
				$("#position").val($tr.find(".position").text());
				$("#company").val($tr.find(".company").text());
				$("#salary").val($tr.find(".salary").text());
				$("#address").val($tr.find(".address").text());
				$("#exprience").val($tr.find(".exprience").text());
				$("#type").val($tr.find(".type").text());
			},
			editHandle(){
				const url = "/api/position/edit";
				const formData = new FormData($(".editform")[0]);
				$.ajax({
					url:url,
					data:formData,
					type:"post",
					processData:false,
					contentType:false,
					success:function(res){
						console.log(res)
						if(res.res_body.status === 1){
							$(".edit-succ").removeClass("hidden");
							function dissp(){
								$("#editPosition").modal('hide');
								window.location.reload();
							}
							setTimeout(dissp, 1500);
						}
					}
				})
			}

		})
		return new Index();
	})
})